﻿#include <easyx.h>
#include <stdio.h>
#include <stdlib.h>
//因为这三个对象都有draw方法，我们可以将其抽象出来
// 
//因为下边的结构体使用Shape都在往上找，所以要把这个结构体放在最上边。
struct Shape
{
	void(*draw) (struct Shape*);//共性方法
};

//c语言面向对象
//矩形
struct Rect
	
{
	struct Shape super;
	int left;
	int top;
	int right;
	int bottom;
	//void (*draw)(struct Rect*);
};

//圆形
struct Circle
{
	struct Shape super;

	int x;
	int y;
	int r;
	//void (*draw)(struct  Circle*);

};
//三角形
struct Triangle
{
	struct Shape super;
	POINT p1;
	POINT p2;
	POINT p3;
	//void (*draw)(struct Triangle*);//封装
	

};

void drawRect(struct Rect* r)
{
	rectangle(r->left, r->top, r->right, r->bottom);
}

void drawCircle(struct Circle* c)
{
	circle(c->x, c->y, c->r);
}

void drawTriangle(struct Triangle* t)
{
	line(t->p1.x, t->p1.y, t->p2.x, t->p2.y);
	line(t->p2.x, t->p2.y, t->p3.x, t->p3.y);
	line(t->p3.x, t->p3.y, t->p1.x, t->p1.y);
}

void InitRect(struct Rect* r)
{
	r->super.draw = (void(*)(struct Shape*))drawRect;
}

void InitCircle(struct Circle* c)
{
	c->super.draw = (void(*)(struct Shape*))drawCircle;
}
void InitTriangle(struct Triangle* t)
{
	t->super.draw = (void(*)(struct Shape*))drawTriangle;
}

int main()
{
	initgraph(800, 600);
	setaspectratio(1, -1);
	setorigin(400, 300);

	setbkcolor(WHITE);
	setlinecolor(BLACK);
	cleardevice();

	struct Rect r = { {}, - 200,200,200,0 };
	struct Circle c = { {},0,0,100 };
	struct Triangle t = { {}, {0,200},{-200,0},{200,0} };

	InitRect(&r);
	InitCircle(&c);
	InitTriangle(&t);

	struct Shape* arrShape[3] = {(struct Shape*)&r,(struct Shape*)&c,(struct Shape*)&t};
	for (int i = 0; i < 3; i++)
	{
		arrShape[i]->draw(arrShape[i]);
	}
	getchar();
	closegraph();
	return 0;
}


